package org.artifact.core.server.bootstrap;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.artifact.core.constant.ArtifactAttrConstant;
import org.artifact.core.server.*;
import org.artifact.core.server.mirror.DefaultServerMirror;
import org.artifact.core.server.mirror.ServerMirror;

import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 默认引导
 *
 * @author apple
 */
public class DefaultServerBootstrap implements ServerBootstrap {


    static final Log log = LogFactory.get(DefaultServerBootstrap.class);


    @Override
    public ServerMirror getServerMirror() {
        // 默认镜像
        return new DefaultServerMirror();
    }

    @Override
    public void start(String[] args) {
        if (getServerMirror() == null) {
            // 没有配置引导器
            throw new RuntimeException("not find bootstrap");
        }

        beforeStart();
        ServerConfig serverConfig = ServerConfig.me();
        // 1.装载配置
        getServerMirror().config(serverConfig);


        Map<String,Object> attribute = (Map) serverConfig.getObj("attribute",new HashMap<>());


        // 2.初始化上下文
        ServerContext serverContext = ServerContext.me();
        getServerMirror().context(serverContext);

        // 配置文件属性
        for (Map.Entry<String, Object> entry : attribute.entrySet()) {
            serverContext.getAttribute().put(entry.getKey(), entry.getValue());
        }

        // 启动命令属性
        for (String arg : args) {
            if (StrUtil.contains(arg, "=")) {
                String[] attrs = StrUtil.splitToArray(arg, '=', 2);
                serverContext.getAttribute().put(attrs[0], attrs[1]);
            } else {
                serverContext.getAttribute().put(arg, "");
            }
        }



        // 3.装载插件
        getServerMirror().plugin(ServerPlugins.me()).start();

        // 4.启动完毕后调用
        afterStart();

        // 5.添加一个停止钩子
        Runtime.getRuntime().addShutdownHook(new ShutdownHook(this));

        // 6.打印banner
        URL url = ResourceUtil.getResourceObj("artifact.banner").getUrl();
        log.info(FileUtil.readString(url, CharsetUtil.UTF_8));

        // 设置启动时间
        ServerContext.me().getAttribute().put(ArtifactAttrConstant.START_DATE,new Date());

        // 标记服务器状态
        ServerContext.me().setStatus(ServerStatusEnum.RUNNING);
    }

    @Override
    public void stop() {
        // 1.停止前调用
        beforeStop();

        // 2.停止插件
        ServerPlugins.me().stop();

        // 3.停止后调用
        afterStop();

        // 4.打印banner
        URL url = ResourceUtil.getResourceObj("bye.banner").getUrl();
        log.info(FileUtil.readString(url, CharsetUtil.UTF_8));
    }


    @Override
    public void beforeStart() {
        log.info("beforeStart");
    }

    @Override
    public void afterStart() {
        log.info("afterStart");
    }

    @Override
    public void beforeStop() {
        log.info("beforeStop");
    }

    @Override
    public void afterStop() {
        log.info("afterStop");
    }
}
